package com.zzy.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzy.product.entity.BrandEntity;
import com.zzy.product.entity.CategoryEntity;
import com.zzy.product.mapper.BrandMapper;
import com.zzy.product.mapper.CategoryMapper;
import com.zzy.product.service.BrandService;
import com.zzy.utils.utils.PageUtils;
import com.zzy.utils.utils.Query;
import com.zzy.product.mapper.CategoryBrandRelationMapper;
import com.zzy.product.entity.CategoryBrandRelationEntity;
import com.zzy.product.service.CategoryBrandRelationService;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author KAZU
 */
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationMapper, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

	@Autowired
	private BrandMapper brandMapper;
	
	@Autowired
	private CategoryMapper categoryMapper;
	
	@Autowired
	private CategoryBrandRelationMapper relationMapper;
	
	@Autowired
	private BrandService brandService;
	
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }
	
	@Override
	public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
		Long brandId = categoryBrandRelation.getBrandId();
		Long catelogId = categoryBrandRelation.getCatelogId();
		/*查询详细名字*/
		BrandEntity brandEntity = brandMapper.selectById(brandId);
		CategoryEntity categoryEntity = categoryMapper.selectById(catelogId);
		categoryBrandRelation.setBrandName(brandEntity.getName());
		categoryBrandRelation.setCatelogName(categoryEntity.getName());
		this.save(categoryBrandRelation);
	}
	
	@Override
	public void updateBrand(Long brandId, String name) {
		CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
		relationEntity.setBrandId(brandId);
		relationEntity.setBrandName(name);
		this.update(relationEntity,
				new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
	}
	
	@Override
	public void updateCategory(Long catId, String name) {
		this.baseMapper.updateCategory(catId, name);
	}
	
	@Override
	public List<BrandEntity> getBrandsByCatId(Long catId) {
		List<CategoryBrandRelationEntity> catelogId = relationMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>()
				.eq("catelog_id", catId));
		List<BrandEntity> collect = catelogId.stream().map((item) -> {
			Long brandId = item.getBrandId();
			BrandEntity byId = brandService.getById(brandId);
			return byId;
		}).collect(Collectors.toList());
		return collect;
	}
	
}















